#include "tmuserctrl.h"
#include "tmglobaldef.h"

TMUserCtrl::TMUserCtrl(QObject *parent):QObject(parent)
{
    MyTimerTaskPtr mytask = new MyTImerTask();
    IceUtil::TimerPtr timer = new IceUtil::Timer();
    //timer->schedule(mytask, IceUtil::Time::seconds(10));           // 10秒后执行任务
    timer->scheduleRepeated(mytask, IceUtil::Time::seconds(8));   // 每隔8秒执行一次
    //timer->cancel(mytask);  // 取消定时器的执行
    //timer->destory(); // 销毁所有定时器
}

std::string TMUserCtrl::send(const std::string &userName, const std::string &mac, const std::string &content, const Ice::Current &)
{
    //NetWorkRobot::CallBackPrx *callBack = pMySqlCtrl->queryCallBackPrx(stdToQString(mac));
    if(robotMap.empty()){
        g_pLog->log_E("robot map is empty!");
        return "";
    }
    if(!m_OnlineUserMap.contains(stdToQString(userName))){
        g_pLog->log_E("this user is not connected!");
        return "";
    }
    if(!robotMap.contains(stdToQString(mac))){
        g_pLog->log_E("robot mac is invalid!");
        return "";
    }
    std::string retMsg;
    NetWorkRobot::CallBackPrx callBack = robotMap.value(stdToQString(mac));
    g_pLog->log_D(QString("the 1 time is:%1").arg(QTime::currentTime().toString("hh:mm:ss.zzz")));
    try{
        retMsg = callBack->sendMsg(content);
        g_pLog->log_D(QString("the 2 time is:%1").arg(QTime::currentTime().toString("hh:mm:ss.zzz")));
//        callBack->begin_sendMsg(content);
        g_pLog->log_D(QString("user send  msg:%1").arg(content.c_str()));
    }catch(Ice::Exception e){
        qDebug()<<"error:"<<e.what();
        g_pLog->log_E(QString("send  msg error:%1").arg(e.what()));
        return "";
    }

    return retMsg;
}

bool TMUserCtrl::connRobot(const std::string &userName, const std::string &mac, const Ice::Current &)
{
    m_OnlineUserMap.insert(stdToQString(userName),stdToQString(mac));
    pMySqlCtrl->saveCurUserName(stdToQString(mac),stdToQString(userName));
    //robotMap.value(stdToQString(mac))->ice_ping();
//    MyTimerTaskPtr mytask = new MyTImerTask();
//    IceUtil::TimerPtr timer = new IceUtil::Timer();
//    timer->schedule(mytask, IceUtil::Time::milliSeconds(100));// 10秒后执行任务

    return true;
}

void TMUserCtrl::robotList(const std::string &userName, Ice::StringSeq &list, const Ice::Current &)
{
    pMySqlCtrl->queryRobotList(stdToQString(userName),list);
}

bool TMUserCtrl::disConnRobot(const std::string &userName, const std::string &mac, const Ice::Current &)
{
    if(!m_OnlineUserMap.contains(stdToQString(userName))){
        g_pLog->log_E("this connect is nonexistent!");
        return false;
    }
    m_OnlineUserMap.remove(stdToQString(userName));
    pMySqlCtrl->saveCurUserName(stdToQString(mac),"");
    return true;
}


void MyTImerTask::runTimerTask()
{
    if(!robotMap.isEmpty()){
        for(int i = 0;i < robotMap.size();i++){
            robotMap.values().at(i)->ice_ping();
            g_pLog->log_D(QString("%1 send callback ping...").arg(robotMap.key(robotMap.values().at(i))));
        }
    }
}
